/*
 * @Project Name: shoplook
 * @File Name: MaterialController
 * @Package Name: com.sm.shoplook.controller.system
 * @Date: 2018/8/13 14:06
 * @Creator: linshixing-1199
 * @line------------------------------
 * @修改人:
 * @修改时间:
 * @修改内容:
 */

package com.sm.shoplook.controller.star;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.domain.star.StarCategory;
import com.sm.shoplook.service.star.StarCategoryService;
import com.sm.shoplook.util.FileUtil;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.base.BaseSearchVo;

/**
 * @author chenpy
 * @desc 明星款分类管理
 * @date 2018/8/13 14:06
 * @see
 */
@Controller
@RequestMapping("/star/category")
public class StarCategoryController {

	private static final Logger LOGGER = LoggerFactory.getLogger(StarCategoryController.class);
	@Autowired
	private StarCategoryService starCategoryService;
	@Autowired
	private FileUtil fileUtil;

	@RequestMapping(value = "/manager", method = RequestMethod.GET)
	public String starManger() {
		return "/star/star_category_manager";
	}

	/**
	 * @param baseSearchVo
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 明星款分类列表
	 */
	@RequestMapping(value = "/pageList", method = RequestMethod.POST)
	@ResponseBody
	public ApiResult<Map<String, Object>> pageList(BaseSearchVo baseSearchVo) {
		Map<String, Object> map = new HashMap<>();
		ApiResult<PageInfo<StarCategory>> pageInfoApiResult = starCategoryService.selectStarCategorysPage(baseSearchVo);
		PageInfo<StarCategory> pageInfo = pageInfoApiResult.getData();
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, map);
	}

	/**
	 * @param categoryId
	 * @param model
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 跳转素材分类保存页面
	 */
	@RequestMapping(value = "/save_page/{categoryId}", method = RequestMethod.GET)
	public String addOrUpdateRolePage(@PathVariable(value = "categoryId") Integer categoryId, Model model) {
		LOGGER.info("categoryId: {}", categoryId);
		ApiResult<StarCategory> materialApiResult = starCategoryService.selectStarCategoryById(categoryId);
		StarCategory starCategory = materialApiResult.getData();
		if (starCategory == null) {
			starCategory = new StarCategory();
		}
		model.addAttribute("starCategory", starCategory);
		return "/star/add_update_star_category";
	}

	/**
	 * @param ids
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 删除明星款分类
	 */
	@ResponseBody
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public ApiResult batchDelete(String ids) {
		String[] idList = ids.split(",");
		// 批量删除
		LOGGER.info("ids", ids);
		try {
			return starCategoryService.batchDelete(idList);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e:{}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}

	/**
	 * @param starCategory
	 * @param imgUrlFile
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 保存
	 */
	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ApiResult save(StarCategory starCategory, MultipartFile imgUrlFile) {
		LOGGER.info("starCategory", starCategory);
		try {
			String filePath = fileUtil.uploadFile(imgUrlFile);
			starCategory.setImgUrl(filePath);
			return starCategoryService.saveStarCategory(starCategory);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}
}
